package de.au.fi.manager;

import static ch.lambdaj.Lambda.filter;
import static ch.lambdaj.Lambda.having;
import static ch.lambdaj.Lambda.on;
import static org.hamcrest.Matchers.*;

import java.util.List;

import org.ektorp.CouchDbConnector;
import org.ektorp.support.CouchDbRepositorySupport;
import org.ektorp.support.GenerateView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import de.au.fi.calc.results.ResultFactory;
import de.au.fi.domain.Stock;
import de.au.fi.domain.Stock.CapType;

@Component
@Service
public class SearchManager extends CouchDbRepositorySupport<Stock> {

	@Autowired
	public SearchManager(@Qualifier("stockDatabase") CouchDbConnector db) {
		super(Stock.class, db);
		initStandardDesignDocument();
	}
	
	@GenerateView
	public List<Stock> findByCapType(CapType capType) {
	    return queryView("by_capType", capType.toString());
	}

	public List<Stock> getStocksWithResult(int result)
	{
		List<Stock> allStocks = getAll();
		ResultFactory.addResults(allStocks);
		return filter(having(on(Stock.class).getResult().getResult(), greaterThan(result)), allStocks);
	}
	
	
}
